package com.health.mapp.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.health.db.model.PatientList;
import com.health.db.model.PatientListExample;
import com.health.db.model.PatientListExample.Criteria;
import com.health.db.service.user.IPatientListService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.ModifyPatientReq;
import com.health.mapp.bean.ModifyPatientResp;
import com.health.mapp.utils.Response;

public class ModifyPatientImpl implements ActionInterface{
	
	private static Logger logger=Logger.getLogger(ModifyPatientImpl.class);
	private IPatientListService patientListService ;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		// TODO Auto-generated method stub
		ModifyPatientReq req=(ModifyPatientReq) reqBean;
		ModifyPatientResp _resp=new ModifyPatientResp();
		
		String patName=req.getPatName();
		if(null==patName||patName.equals(""))
		{
			_resp.setErrorCode(-1);
			_resp.setErrorMsg("就诊人姓名不能为空");
			/**
			 *就诊人姓名长度判断
			 **/
		}else{
			long patId=req.getPatId();
			short patSexId=req.getPatSex();
			String patBirthday=req.getPatBirthday();
			String patSex=patSexId==0?"女":"男";
			
			PatientList patientList=new PatientList();
			patientList.setPatientid(patId);
			patientList.setPatientName(patName);
			DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
			patientList.setBirthdayDate(dateFormat.parse(patBirthday));
			patientList.setSexid(patSexId);
			patientList.setSex(patSex);
			patientList.setUpdateDate(new Date());
			
			PatientListExample example=new PatientListExample();
			Criteria criteria=example.createCriteriaInternal();
			criteria.andPatientidEqualTo(patId);
			example.or(criteria);
			
			
			int result=patientListService.updatePatientList(patientList, example);
			if (result>=0) {
				_resp.setErrorCode(0);
				_resp.setErrorMsg("Success");
			}else
			{
				_resp.setErrorCode(-1);
				_resp.setErrorMsg("更新就诊人信息失败");
			}
		}
		return _resp;
	}

	public IPatientListService getPatientListService() {
		return patientListService;
	}

	public void setPatientListService(IPatientListService patientListService) {
		this.patientListService = patientListService;
	}

	
	

}
